package fr.lelouet.server.perf.snapshot.receivers;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.Map;

import fr.lelouet.server.perf.snapshot.DirectoryReader;
import fr.lelouet.server.perf.snapshot.HVSnapshot;
import fr.lelouet.server.perf.snapshot.SharedMainConfig;

/**
 * receive {@link HVSnapshot} , and on {@link #write()} generates a file of name
 * {@link #fileName}.{@link #FILE_EXTENSION} in the csv format that contains the
 * global resources consumptions of the snapshots memorized
 */
public class CSVExporter extends TableLoader {

	public static final String FILE_EXTENSION = ".csv";

	public CSVExporter() {
		reset();
	}

	/**
	 * set the name of the file to use. Automatically append
	 * {@value #FILE_EXTENSION} to the name
	 */
	public void setFileName(String filename) {
		fileName = filename;
	}

	/** the name to save the file as */
	protected String fileName = "snapshots" + FILE_EXTENSION;

	public static final String NEWLINE = System.getProperty("line.separator");
	public static final String CSV_SEPARATOR = ";";
	public static final String FLANAGAN_SEPARATOR = " ";

	public void write() throws IOException {
		write(false);
	}

	/**
	 * @param flanagan
	 *            if set to true, write with the flanagan parsing
	 */
	public void write(boolean flanagan) throws IOException {
		BufferedWriter buff = new BufferedWriter(new FileWriter(new File(
				fileName)));
		StringBuilder line = new StringBuilder();
		String separator = flanagan ? FLANAGAN_SEPARATOR : CSV_SEPARATOR;
		if (flanagan) {
			buff.write("pcaforenergy" + NEWLINE);
			buff.write("" + events.size() + NEWLINE);
			buff.write("" + loadedData.size() + NEWLINE);
		}
		for (String key : events) {
			if (line.length() != 0) {
				line.append(separator);
			}
			line.append(key);
		}
		line.append(NEWLINE);
		buff.write(line.toString());
		for (Map<String, Double> data : loadedData) {
			line = new StringBuilder();
			for (String key : events) {
				if (line.length() != 0) {
					line.append(separator);
				}
				line.append(data.get(key));
			}
			line.append(NEWLINE);
			buff.write(line.toString());
		}
		buff.close();
	}

	/** accept only the .csv files */
	protected static final FilenameFilter csvAcceptor = new FilenameFilter() {
		@Override
		public boolean accept(File dir, String name) {
			boolean ret = name.endsWith(CSVExporter.FILE_EXTENSION);
			return ret;
		}
	};

	/**
	 * for each directory in ./snapshots, loads snapshots in them and create a
	 * csv file if there is not one yet
	 */
	public static void main(String args[]) throws IOException {
		DirectoryReader reader = new DirectoryReader();
		File snapsResDir = new File(SharedMainConfig.snapshotsDir);
		CSVExporter exporter = new CSVExporter();
		reader.register(exporter);
		for (File child : snapsResDir.listFiles()) {
			if (child.isDirectory()
					&& child.getName().startsWith(DirectoryWriter.DIR_PREFIX)
					&& child.list(csvAcceptor).length == 0) {
				System.out.println("exporting from dir "
						+ child.getAbsolutePath());
				reader.setDirPath(child.getAbsolutePath());
				reader.reset();
				exporter.setFileName(child.getAbsolutePath() + "/human.csv");
				exporter.reset();
				reader.loadNewFiles();
				exporter.write();
				exporter.setFileName(child.getAbsolutePath() + "/flanagan.csv");
				exporter.write(true);
			}
		}
	}

}
